package com.pan.admin.module.channel.controller;

import com.gitee.apanlh.util.base.Eq;
import com.pan.admin.common.constant.module.StatisticEnum;
import com.pan.admin.common.constant.module.StatisticEnumVisitEnum;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.module.channel.entity.LoanChannelFormRateStatistic;
import com.pan.admin.module.channel.entity.LoanChannelRateStatistic;
import com.pan.admin.module.channel.entity.LoanChannelStatistic;
import com.pan.admin.module.channel.entity.qo.LoanChannelStatisticQO;
import com.pan.admin.module.channel.entity.vo.LoanChannelFormRateStatisticVO;
import com.pan.admin.module.channel.entity.vo.LoanChannelRateStatisticVO;
import com.pan.admin.module.channel.entity.vo.LoanChannelStatisticVO;
import com.pan.admin.module.channel.service.LoanChannelStatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 渠道分析统计Controller
 *
 * @author ts
 * @date 2023-09-12
 */
@RestController
@RequestMapping("/channel/statistic")
public class LoanChannelStatisticController extends BaseController {

    @Autowired
    private LoanChannelStatisticService loanChannelStatisticService;

    /**
     * 根据渠道获取访问量统计
     *
     * @param channelId
     * @param type
     * @param viewType
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('channel:statistic:visits')")
    @GetMapping("/visits")
    public AjaxResult visits(@RequestParam("channelId") Long channelId,
                             @RequestParam("type") String type,
                             @RequestParam("viewType") String viewType) {
        //  统计类型
        StatisticEnum statisticEnum = StatisticEnum.get(viewType);
        //  获取统计查询类
        LoanChannelStatisticQO statisticQO = statisticEnum.getChannelStatisticQO(channelId);

        List<LoanChannelStatistic> list = null;
        if (StatisticEnumVisitEnum.UV.getType().equals(type)) {
            list = loanChannelStatisticService.listUv(statisticQO);
        } else {
            list = loanChannelStatisticService.listPv(statisticQO);
        }

        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success(list);
        }

        //  转化图表数据
        if (StatisticEnum.NOW.getType().equals(viewType)) {
            return AjaxResult.success(list);
        }

        //  转化统计图表实体
        List<LoanChannelStatisticVO> fill = LoanChannelStatisticVO.fill(list, statisticEnum);
        return AjaxResult.success(fill);
    }

    /**
     * 统计总访问量
     *
     * @param type
     * @param viewType
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('channel:statistic:visits:sum')")
    @GetMapping("/visitsSum")
    public AjaxResult visitsSum(@RequestParam("type") String type,
                                @RequestParam("viewType") String viewType) {
        //  统计类型
        StatisticEnum statisticEnum = StatisticEnum.get(viewType);
        //  获取统计查询类
        LoanChannelStatisticQO statisticQO = statisticEnum.getChannelStatisticQO(null);

        List<LoanChannelStatistic> list = null;
        if (StatisticEnumVisitEnum.UV.getType().equals(type)) {
            list = loanChannelStatisticService.listSumUv(statisticQO);
        } else {
            list = loanChannelStatisticService.listSumPv(statisticQO);
        }

        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success(list);
        }

        //  转化统计图表实体
        List<LoanChannelStatisticVO> fill = LoanChannelStatisticVO.fill(list, statisticEnum);
        return AjaxResult.success(fill);
    }

    /**
     * 根据渠道获取表单数量统计
     *
     * @param channelId
     * @param viewType
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('channel:statistic:userForm')")
    @GetMapping("/userForm")
    public AjaxResult userForm(@RequestParam("channelId") Long channelId,
                               @RequestParam("viewType") String viewType) {
        //  统计类型
        StatisticEnum statisticEnum = StatisticEnum.get(viewType);
        //  获取统计查询类
        LoanChannelStatisticQO statisticQO = statisticEnum.getChannelStatisticQO(channelId);
        List<LoanChannelStatistic> list = loanChannelStatisticService.listUserForm(statisticQO);

        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success(list);
        }

        //  转化图表数据
        if (StatisticEnum.NOW.getType().equals(viewType)) {
            return AjaxResult.success(list);
        }

        //  转化统计图表实体
        List<LoanChannelStatisticVO> fill = LoanChannelStatisticVO.fill(list, statisticEnum);
        return AjaxResult.success(fill);
    }

    /**
     * 根据渠道获取注册数量统计
     *
     * @param channelId
     * @param viewType
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('channel:statistic:userRegister')")
    @GetMapping("/userRegister")
    public AjaxResult userRegister(@RequestParam("channelId") Long channelId,
                                   @RequestParam("viewType") String viewType) {
        //  统计类型
        StatisticEnum statisticEnum = StatisticEnum.get(viewType);
        //  获取统计查询类
        LoanChannelStatisticQO statisticQO = statisticEnum.getChannelStatisticQO(channelId);
        List<LoanChannelStatistic> list = loanChannelStatisticService.listUserRegister(statisticQO);

        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success(list);
        }

        //  转化图表数据
        if (StatisticEnum.NOW.getType().equals(viewType)) {
            return AjaxResult.success(list);
        }

        //  转化统计图表实体
        List<LoanChannelStatisticVO> fill = LoanChannelStatisticVO.fill(list, statisticEnum);
        return AjaxResult.success(fill);
    }

    /**
     * 根据渠道获取老用户数量统计
     *
     * @param channelId
     * @param viewType
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('channel:statistic:userOld')")
    @GetMapping("/userOld")
    public AjaxResult userOld(@RequestParam("channelId") Long channelId,
                              @RequestParam("viewType") String viewType) {
        //  统计类型
        StatisticEnum statisticEnum = StatisticEnum.get(viewType);
        //  获取统计查询类
        LoanChannelStatisticQO statisticQO = statisticEnum.getChannelStatisticQO(channelId);
        List<LoanChannelStatistic> list = loanChannelStatisticService.listUserOld(statisticQO);

        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success(list);
        }

        //  转化图表数据
        if (StatisticEnum.NOW.getType().equals(viewType)) {
            return AjaxResult.success(list);
        }

        //  转化统计图表实体
        List<LoanChannelStatisticVO> fill = LoanChannelStatisticVO.fill(list, statisticEnum);
        return AjaxResult.success(fill);
    }

    /**
     * 根据渠道获取老用户数量统计
     *
     * @param channelId
     * @param viewType
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('channel:statistic:userMarket')")
    @GetMapping("/userMarket")
    public AjaxResult userMarket(@RequestParam("channelId") Long channelId,
                                 @RequestParam("viewType") String viewType,
                                 @RequestParam("type") String type) {
        //  统计类型
        StatisticEnum statisticEnum = StatisticEnum.get(viewType);
        //  获取统计查询类
        LoanChannelStatisticQO statisticQO = statisticEnum.getChannelStatisticQO(channelId);
        String single = "0";
        String sum = "1";

        List<LoanChannelStatistic> list = null;
        if (Eq.str(single, type)) {
            list = loanChannelStatisticService.listUserMarket(statisticQO);
        }
        if (Eq.str(sum, type)) {
            list = loanChannelStatisticService.listMarketSum(statisticQO);
        }

        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success(list);
        }

        //  转化图表数据
        if (StatisticEnum.NOW.getType().equals(viewType)) {
            return AjaxResult.success(list);
        }

        //  转化统计图表实体
        List<LoanChannelStatisticVO> fill = LoanChannelStatisticVO.fill(list, statisticEnum);
        return AjaxResult.success(fill);
    }

    /**
     * 根据渠道获取注册率/表单率
     *
     * @param channelId
     * @param viewType
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('channel:statistic:baseRate')")
    @GetMapping("/baseRate")
    public AjaxResult baseRate(@RequestParam("channelId") Long channelId,
                               @RequestParam("viewType") String viewType) {
        //  统计类型
        StatisticEnum statisticEnum = StatisticEnum.get(viewType);
        //  获取统计查询类
        LoanChannelStatisticQO statisticQO = statisticEnum.getChannelStatisticQO(channelId);
        List<LoanChannelRateStatistic> list = loanChannelStatisticService.listBaseRate(statisticQO);

        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success(list);
        }

        //  转化统计图表实体
        List<LoanChannelRateStatisticVO> fill = LoanChannelRateStatisticVO.fill(list, statisticEnum);
        return AjaxResult.success(fill);
    }

    /**
     * 根据总获取注册率/表单率
     *
     * @param viewType
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('channel:statistic:baseRate:sum')")
    @GetMapping("/baseRateSum")
    public AjaxResult baseRate(@RequestParam("viewType") String viewType) {
        //  统计类型
        StatisticEnum statisticEnum = StatisticEnum.get(viewType);
        //  获取统计查询类
        List<LoanChannelRateStatistic> list = loanChannelStatisticService.listBaseRateSum(statisticEnum.getChannelStatisticQO(null));
        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success(list);
        }
        //  转化统计图表实体
        List<LoanChannelRateStatisticVO> fill = LoanChannelRateStatisticVO.fill(list, statisticEnum);
        return AjaxResult.success(fill);
    }

    /**
     * 根据渠道获取表单资质
     *
     * @param channelId
     * @param viewType
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('channel:statistic:formRate')")
    @GetMapping("/formRate")
    public AjaxResult formRate(@RequestParam("channelId") Long channelId,
                               @RequestParam("viewType") String viewType) {
        //  统计类型
        StatisticEnum statisticEnum = StatisticEnum.get(viewType);
        //  获取统计查询类
        LoanChannelStatisticQO statisticQO = statisticEnum.getChannelStatisticQO(channelId);
        List<LoanChannelFormRateStatistic> list = loanChannelStatisticService.listFormRate(statisticQO);
        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success(list);
        }
        //  转化统计图表实体
        List<LoanChannelFormRateStatisticVO> fill = LoanChannelFormRateStatisticVO.fill(list, statisticEnum);
        return AjaxResult.success(fill);
    }
}
